import { _decorator, Component, Node, BoxCollider, ITriggerEvent } from 'cc';
import { constant } from '../constant';
import { gameManeger } from '../framework/gameManeger';
import { PoolManager } from '../framework/PoolManager';
import { planeController } from '../planeController';
const { ccclass, property } = _decorator;

@ccclass('BulletProp')
export class BulletProp extends Component {
    private _player: planeController;
    private _speedZ = 0;
    private _speedX = 0;
    onEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.on("onTriggerEnter", this._onTriggerEnter, this);
    }
    onDisEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.off("onTriggerEnter", this._onTriggerEnter, this);
    }
    start() {

    }

    update(deltaTime: number) {
        let pos = this.node.position; {
            if (pos.x > 15) {
                this._speedX = -this._speedX;
            }
            else if (pos.x < -15) {
                this._speedX = -this._speedX;
            }
            this.node.setPosition(pos.x + this._speedX, pos.y, pos.z + this._speedZ);
            pos = this.node.position;
            if (pos.z > 50) {
                //this.node.destroy();
                PoolManager.instance().putNode(this.node);
                console.log("pd");
            }
        }
    }
    private _onTriggerEnter(event: ITriggerEvent) {
        // const name = event.selfCollider.name;
        // if (name == "bulletH") {
        //     this._player.changeBulletType(constant.BulletType.BULLET_H);
        //     this.node.destroy();
        // }
        // else if (name == "bulletS") {
        //     this._player.changeBulletType(constant.BulletType.BULLET_S);
        //     this.node.destroy();
        // }
        // else {
        //     this._player.changeBulletType(constant.BulletType.BULLET_M);
        //     this.node.destroy();
        // }
        //this.node.destroy();
        PoolManager.instance().putNode(this.node);
        console.log("pd");
    }
    public setPlayer(_player: planeController) {
        this._player = _player;
    }
    show(speedX: number, speedZ: number) {
        this._speedX = speedX;
        this._speedZ = speedZ;
    }
}

